home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-05 | 2.7 KB | 131 lines | [MATS/MATL] |
- echo off;
- % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
- % To accompany the text:
- % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
- % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
- % This free software is complements of the author.
-
- % Algorithm 10.2 (Forward-Difference Method for the Heat Equation).
- % Section 10.2, Parabolic Equations, Page 516
- echo on; clc; format short; hold off; clear
-
- % PARABOLIC EQUATIONS.
- % Forward difference solution for the heat equation
-
- % 2
- % u (x,t) = c u (x,t)
- % t xx
-
- % u(x,0) = f(x) for 0 < x < a and
-
- % u(0,t) = g1(t) and u(a,t) = g2(t) for 0 ≤ t ≤ b
-
- % A numerical approximation is computed over the rectangle
-
- % 0 ≤ x ≤ a , 0 ≤ t ≤ b.
-
- pause % Press any key to continue.
-
- clc;
- % Store f(x), g1(x), g2(x) in f.m g1.m g2.m
- % function z = f(x)
- % z = 4*x - 4*x^2;
-
- % function z = g1(t)
- % z = 0;
-
- % function z = g2(t)
- % z = 0;
-
- delete f.m
- diary f.m; disp('function z = f(x)');...
- disp('z = 4*x - 4*x^2;');...
- diary off;
-
- delete g1.m
- diary g1.m; disp('function z = g1(t)');...
- disp('z = 0;');...
- diary off;
-
- delete g2.m
- diary g2.m; disp('function z = g2(t)');...
- disp('z = 0;');...
- diary off;
-
- % Remark. f.m g1.m g2.m forwdif.m are used for Algorithm 10.2
- f(0); g1(0); g2(0);
- pause % Press any key to continue.
-
- clc;
- % Place the endpoint of [0,a] in a.
-
- % Place the endpoint of [0,b] in b.
-
- % For the heat equation, enter the constant c.
-
- % Over [0,a] enter the number of grid points n.
-
- % Over [0,b] enter the number of grid points m.
-
- a = 1.0;
- b = 0.2;
- c = 1;
- n = 6;
- m = 11;
-
- U = forwdif('f','g1','g2',a,b,c,n,m);
-
- pause % Press any key to see the solution.
-
- clc; clg;
- Z = fliplr(U);
- mesh(Z);...
- Mx1 = 'The forward difference solution to the heat equation.';...
- title(Mx1);...
- shg; pause % Press any key to continue.
-
- W = U';
- points = W(:,2:n-1);
- Mx2 = 'The forward difference method is stable because r <= 1/2.';
- clc,echo off,diary output,disp(' '),...
- disp(Mx1),disp(' '),disp(points),disp(Mx2),...
- diary off,echo on
-
-
- pause % Press any key to continue.
-
- clc;
- % Place the endpoint of [0,a] in a.
-
- % Place the endpoint of [0,b] in b.
-
- % For the heat equation, enter the constant c.
-
- % Over [0,a] enter the number of grid points n.
-
- % Over [0,b] enter the number of grid points m.
-
- a = 1.0;
- b = 0.333333333;
- c = 1;
- n = 6;
- m = 11;
-
- U = forwdif('f','g1','g2',a,b,c,n,m);
-
- pause % Press any key to see the solution.
-
- clc; clg;
- Z = fliplr(U);
- mesh(Z);
- title(Mx1)
-
- W = U';
- points = W(:,2:n-1);
- Mx3 = 'The forward difference method is unstable because r > 1/2.';
- clc,echo off,diary output,disp(' '),...
- disp(Mx1),disp(' '),disp(points),disp(Mx3),...
- diary off,echo on
-
-
-